const {ipcRenderer} = require('electron')

;(function () {
    document.getElementById("but_submit").addEventListener('click', () => {
        if (validate()) {
            let name = document.getElementById("user").value
            let password = document.getElementById("passwd").value
            axios.post("http://localhost:9000/register", {
                username: name,
                password: password
            }).then(function (response) {
                if (response.status == 200) {
                    alert("成功注册！")
                    ipcRenderer.send('register-success')
                }
            }).catch(function (error) {
                if (error.response.status == 403) {
                    alert("此用户已存在！")
                    ipcRenderer.send('register-fail')
                }
            })
        } else {
            ipcRenderer.send('register-fail')
        }
    })
})();

function validate(){

    const inputCode = document.getElementById("Captcha").value.toUpperCase();

    if(inputCode.length <= 0) {
        alert("验证码错误")
        return false;
    }else if(inputCode !== code ) {
        alert("验证码错误")
        createCode();
        document.getElementById("Captcha").value = "";//清空文本框
        return false;
    }else {
        return true
    }
}
